<?php namespace Common\Model;
/**
 * 品牌模板
 */
use Hdphp\Model\Model;
class Brand extends Model
{
	protected $table = "brand";
	//自动验证
	protected $validate = array(
	   array('brand_name','required','品牌名称不能为空',3,3),
	   array('brand_sort','required','品牌序号不能为空',3,3),
	);
	//自动完成
	protected $auto = array(
	   array('brand_logo','image','method',3,3),
	);
	/**
	 * 处理logo图片
	 */
	public function image()
	{
		//如果有隐藏域里面的【brand_logo】,直接把隐藏域里面路径反出
		if(isset($_POST['brand_logo']))
		{
			return $_POST['brand_logo'];
		}
		//设置上传路径
		$path = "Upload/" .date('ymd');
		$file = Upload::type('jpg,png')->path($path)->size(2000000)->make();
		//如果有文件上传
		if($file)
		{
			$thumbpath = Image::thumb($file[0]['path'],$path ."/thumb_" . $file[0]['basename'],200,200,5);
			return $thumbpath;
		}else{
			return "";
		}
	}
	
	/**
	 * 添加
	 */
	public function store()
	{
		if(!$this->create()) return false;
		//上传错误信息
		if($_FILES['brand_logo']['error']!=4)
		{
			if($error = Upload::getError())
		    {
			    $this->error = $error;
				return false;
		    }
		}		
		return $this->add();
	}
	/**
	 * 编辑
	 */
	public function edit()
	{
		if(!$this->create()) return false;
		if(isset($_FILES['brand_logo']) && $_FILES['brand_logo']['error']!=4)
		{
			//上传错误信息
			if($error = Upload::getError())
			{
				$this->error = $error;
				return false;
			}
		}
		return $this->save();
	}
}
